package com.iuie.ns.system.maker.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.iuie.ns.frame.utils.StringUtils;
import com.iuie.ns.system.maker.AbstractBatchMaker;
import com.iuie.ns.system.vo.code.SysCodeInfoVO;

public class QuicklyCodeInfoBatchMaker extends AbstractBatchMaker<SysCodeInfoVO> {

	public QuicklyCodeInfoBatchMaker(String rule) {
		super(rule);
	}

	@Override
	public List<SysCodeInfoVO> make() {
		String[] arr = this.text.split("\n"); // 正则表达式分割
		if (arr == null || arr.length == 0) {
			return null;
		}
		List<SysCodeInfoVO> list = new ArrayList<>();
			
		for (String temp: arr) {
			if (StringUtils.isBlank(temp)) {
				continue;
			}
			
			String codeInfoName = regMatch(temp, "name: ([^(,+})]*)"); // 正则表达式获取
			String codeInfoValue = regMatch(temp, "value: ([^(,+})]*)");
			
			SysCodeInfoVO obj = this.getObject();
			obj.setCodeInfoName(codeInfoName);
			obj.setCodeInfoValue(codeInfoValue);
			list.add(obj);
		}
		return list;
	}

	@Override
	protected SysCodeInfoVO getObject() {
		return new SysCodeInfoVO();
	}

	@Override
	protected void setObjectAttribute(SysCodeInfoVO codeInfo, String value, int index) {
		
	}
	
	// 正则表达式获取
	private String regMatch(String text, String reg) {
		Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(text);
        String result = null;
        while (matcher.find()) {
        	result = matcher.group(1);
        	break;
        }
        return result;
	}
	

}
